package com.guoyansoft.controller;

import com.guoyansoft.config.R;
import com.guoyansoft.model.pojo.*;
import com.guoyansoft.model.service.*;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：高凡
 * @date ：Created in 2023-04-26-16:10
 * @description：调度员Controller

 */
@RestController
@RequestMapping("/yardman")
public class YardmanController {
    @Autowired
    private YardmanService yardmanService;
    @Autowired
    private ProductTeamService productTeamService;
    @Autowired
    private DistrictService districtService;
    @Autowired
    private ShopService shopService;
    /**
     * @param yardman 要添加的调度员实体
     * @return 响应结果
     */
    @RequestMapping("save")
    public R save(Yardman yardman, Integer shopId){
        Shop shop=new Shop();
        shop.setId(shopId);
        yardman.setShop(shop);
        R r = yardmanService.save(yardman);

        return r;
    }
     @RequestMapping("saveAll")
     public R saveAll(Integer yardmanId,Integer[] addressData,Integer[] productTeamData){

         for (Integer integer : addressData) {
             R r1 = yardmanService.saveYardmanAndAddress(new YardmanAddress(null, yardmanId, integer));
         }
         for (Integer integer : productTeamData) {
             R r2 = yardmanService.saveYardmanAndProductTeam(new YardmanAndProductTeam(null, yardmanId, integer));

         }
         return R.suc();
     }
    /**
     * @param yardmanId 调度员实体的编号
     * @return 删除记录数
     */
    @RequestMapping("drop")
    public R drop(Integer[] yardmanId){
        R r = yardmanService.drop(yardmanId);
         yardmanService.dropYardmanAndAddress(yardmanId);
         yardmanService.dropYardmanAndProduct(yardmanId);
        return r;
    }

    /**
     * @param yardman 要修改的调度员实体
     * @return 修改记录数
     */
    @RequestMapping("modify")
    public R modify(Yardman yardman, String shopId){
        System.out.println(shopId);
        Shop shop1 = shopService.selectByName(shopId);
        yardman.setShop(shop1);
        R r = yardmanService.modify(yardman);
        return r;
    }

    @RequestMapping("modifyAll")
    public R modifyAll(Integer yardmanId,String[] addressData,String[] productTeamData,
                       String[] addressData2,String[] productTeamData2){

        for (int i = 0; i < addressData.length; i++) {
            System.out.println(addressData2[i]);
            Map<String,Object> map=new HashMap<>();
            map.put("yardmanId",yardmanId);
            map.put("district",addressData2[i]);
            District ordId = yardmanService.queryByDistrict(map);
            District newId = districtService.queryByDistrict(addressData[i]);
            map.put("ordId",ordId.getId());
            map.put("newId",newId.getId());
            R r = yardmanService.modifyYardmanAndAddress(map);
        }
        for (int i = 0; i < productTeamData.length; i++) {
            ProductTeam ordId = productTeamService.queryByName(productTeamData2[i]);
            ProductTeam newId = productTeamService.queryByName(productTeamData[i]);
            Map<String,Object> map=new HashMap<>();
            map.put("yardmanId",yardmanId);
            map.put("ordId",ordId.getId());
            map.put("newId",newId.getId());
            yardmanService.modifyYardmanAndProduct(map);
        }

        return R.suc();
    }

    /**
     * @param yardmanName
     * @param shopName
     * @param curPage
     * @param pageSize 查询的所有条件
     * @return 响应结果
     */
    @RequestMapping("queryByPage")
    public R queryByPage(String yardmanName,String shopName,Integer curPage,Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        System.out.println(  yardmanName);
        map.put("yardmanName","%"+yardmanName.trim()+"%");
        map.put("shopName",shopName);
        map.put("startIndex",(curPage-1)*pageSize);
        map.put("pageSize",pageSize);
        R r = yardmanService.queryByPage(map);
             return r;
    }

    @RequestMapping("queryAll")
    public R queryAll(String shopName,String district,String productName,Integer curPage,Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        map.put("district",district);
        map.put("shopName",shopName);
        map.put("productName",productName);
        map.put("startIndex",(curPage-1)*pageSize);
        map.put("pageSize",pageSize);
        R r = yardmanService.queryAll(map);
        return r;
    }

    @RequestMapping("queryAddress")
    public R queryAddress(Integer id){
        R r = yardmanService.queryAddress(id);

        return r;
    }
    @RequestMapping("login")
    public R login(Yardman yardman){
        R r = yardmanService.selectByLogin(yardman);
        return r;
    }
}
